package com.kuang.demo.service;

import com.fasterxml.jackson.databind.ser.Serializers;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kuang.demo.bean.acgBeanVo.BaseData;
import com.kuang.demo.bean.acgBeanVo.TotalData;
import com.kuang.demo.bean.acgBeanVo.UserAddressVo;
import com.kuang.demo.bo.acg.*;
import com.kuang.demo.mapper.acg.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ACG
 * @date 2020-10-31 15:39
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    UserMapper1 userMapper1;


    @Override
    public BaseData<User> userControl(Integer page, Integer limit, String sort, String order,String username,String  mobile) {

        if (username!=null&&mobile==null&&!("".equals(username))){
            List<User> users = userMapper1.selectByUserName("%" + username + "%");
            PageInfo<User> userPageInfo = new PageInfo<>(users);
            long total = userPageInfo.getTotal();
            return BaseData.data(users,total);
        }else if (username==null&&mobile!=null&&!("".equals(mobile))){
            List<User> users = userMapper1.selectByMobile(mobile);
            PageInfo<User> userPageInfo = new PageInfo<>(users);
            long total = userPageInfo.getTotal();
            return BaseData.data(users,total);
        }
        else if(username!=null&&mobile!=null){
            if (!("".equals(username))&&!("".equals(mobile))){
                username = "%"+username+"%";
                List<User> users = userMapper1.selectByUserNameAndMoblie(username, mobile);
                PageInfo<User> userPageInfo = new PageInfo<>(users);
                long total = userPageInfo.getTotal();
                return BaseData.data(users,total);
            }if (("".equals(username))&&!("".equals(mobile))){
                List<User> users = userMapper1.selectByMobile(mobile);
                PageInfo<User> userPageInfo = new PageInfo<>(users);
                long total = userPageInfo.getTotal();
                return BaseData.data(users,total);
            }else if (!("".equals(username))&&("".equals(mobile))){
                List<User> users = userMapper1.selectByUserName("%" + username + "%");
                PageInfo<User> userPageInfo = new PageInfo<>(users);
                long total = userPageInfo.getTotal();
                return BaseData.data(users,total);
            }else if ("".equals(username)&&"".equals(mobile)){
                PageHelper.startPage(page,limit);
                UserExample userExample = new UserExample();
                //设置order by 后面的内容
                userExample.setOrderByClause(sort +" "+order); //add_time desc
                List<User> users = userMapper.selectByExample(userExample);
                //可以在查询完成后获得pageInfo → 根据查询结果获得
                PageInfo<User> userPageInfo = new PageInfo<>(users);
                long total = userPageInfo.getTotal();//根据sql查询条件执行了一个select count(*)
                return  BaseData.data(users,total);
            }
        }


            PageHelper.startPage(page,limit);
            UserExample userExample = new UserExample();
            //设置order by 后面的内容
            userExample.setOrderByClause(sort +" "+order); //add_time desc
            List<User> users = userMapper.selectByExample(userExample);
            //可以在查询完成后获得pageInfo → 根据查询结果获得
            PageInfo<User> userPageInfo = new PageInfo<>(users);
            long total = userPageInfo.getTotal();//根据sql查询条件执行了一个select count(*)
            return  BaseData.data(users,total);

    }



    @Autowired
    AddressMapper addressMapper;
    @Autowired
    AddressMapper1 addressMapper1;
    @Override
    public UserAddressVo addressControl(Integer page, Integer limit, String sort, String order,String name,String userId) {
        UserAddressVo userAddressVo1 = new UserAddressVo();
        if (name!=null&&userId==null&&!("".equals(name))){
            List<UserAddressVo.ItemsBean> itemsBeans = addressMapper1.selectByName("%" + name + "%");
            PageInfo<UserAddressVo.ItemsBean> addressPageInfo = new PageInfo<>(itemsBeans);
            int total = (int) addressPageInfo.getTotal();
            userAddressVo1.setTotal(total);
            userAddressVo1.setItems(itemsBeans);
            return userAddressVo1;
        }else if (name==null&&userId!=null&&!("".equals(userId))){
            List<UserAddressVo.ItemsBean> itemsBeans = addressMapper1.selectByUserId(userId);
            PageInfo<UserAddressVo.ItemsBean> addressPageInfo = new PageInfo<>(itemsBeans);
            int total = (int) addressPageInfo.getTotal();
            userAddressVo1.setTotal(total);
            userAddressVo1.setItems(itemsBeans);
            return userAddressVo1;
        }
        else if(name!=null&&userId!=null){
            if (!("".equals(name))&&!("".equals(userId))){
                name = "%"+name+"%";
                List<UserAddressVo.ItemsBean> itemsBeans = addressMapper1.selectByNameAndUserId(name, userId);

                PageInfo<UserAddressVo.ItemsBean> addressPageInfo = new PageInfo<>(itemsBeans);
                int total = (int) addressPageInfo.getTotal();
                userAddressVo1.setTotal(total);
                userAddressVo1.setItems(itemsBeans);
                return userAddressVo1;
            }if (("".equals(name))&&!("".equals(userId))){
                List<UserAddressVo.ItemsBean> itemsBeans = addressMapper1.selectByUserId(userId);
                PageInfo<UserAddressVo.ItemsBean> addressPageInfo = new PageInfo<>(itemsBeans);
                int total = (int) addressPageInfo.getTotal();
                userAddressVo1.setTotal(total);
                userAddressVo1.setItems(itemsBeans);
                return userAddressVo1;
            }else if (!("".equals(name))&&("".equals(userId))){
                List<UserAddressVo.ItemsBean> itemsBeans = addressMapper1.selectByName("%" + name + "%");
                PageInfo<UserAddressVo.ItemsBean> addressPageInfo = new PageInfo<>(itemsBeans);
                int total = (int) addressPageInfo.getTotal();
                userAddressVo1.setTotal(total);
                userAddressVo1.setItems(itemsBeans);
                return userAddressVo1;
            }else if ("".equals(name)&&"".equals(userId)){
                PageHelper.startPage(page,limit);
                List<UserAddressVo.ItemsBean> userAddressVoList = addressMapper1.selectByExample();
                //可以在查询完成后获得pageInfo →根据查询结果获得
                PageInfo<UserAddressVo.ItemsBean> userAddressVoPageInfo = new PageInfo<>(userAddressVoList);
                userAddressVo1.setItems(userAddressVoList);
                int total = (int) userAddressVoPageInfo.getTotal();
                userAddressVo1.setTotal(total);
                return userAddressVo1;
            }
        }
        PageHelper.startPage(page,limit);
        List<UserAddressVo.ItemsBean> userAddressVoList = addressMapper1.selectByExample();
        //可以在查询完成后获得pageInfo →根据查询结果获得
        PageInfo<UserAddressVo.ItemsBean> userAddressVoPageInfo = new PageInfo<>(userAddressVoList);
        userAddressVo1.setItems(userAddressVoList);
        int total = (int) userAddressVoPageInfo.getTotal();
        userAddressVo1.setTotal(total);
        return userAddressVo1;
    }


    @Autowired
    CollectMapper collectMapper;
    @Autowired
    CollectMapper1 collectMapper1;

    @Override
    public BaseData collectControl(Integer page, Integer limit, String sort, String order, String userId, String valueId) {
        if (valueId!=null&&userId==null&&!("".equals(valueId))){
            List<Collect> collects = collectMapper1.selectByValueId(valueId);
            PageInfo<Collect> collectPageInfo = new PageInfo<>(collects);
            long total = collectPageInfo.getTotal();
            return BaseData.data(collects,total);

        }else if (valueId==null&&userId!=null&&!("".equals(userId))){
            List<Collect> collects = collectMapper1.selectByUserId(userId);
            PageInfo<Collect> collectPageInfo = new PageInfo<>(collects);
            long total = collectPageInfo.getTotal();
            return BaseData.data(collects,total);
        }
        else if(valueId!=null&&userId!=null){
            if (!("".equals(valueId))&&!("".equals(userId))){
                List<Collect> collects = collectMapper1.selectByValueIdAndUserId(valueId, userId);
                PageInfo<Collect> collectPageInfo = new PageInfo<>(collects);
                long total = collectPageInfo.getTotal();
                return BaseData.data(collects,total);
            }if (("".equals(valueId))&&!("".equals(userId))){
                List<Collect> collects = collectMapper1.selectByUserId(userId);
                PageInfo<Collect> collectPageInfo = new PageInfo<>(collects);
                long total = collectPageInfo.getTotal();
                return BaseData.data(collects,total);
            }else if (!("".equals(valueId))&&("".equals(userId))){
                List<Collect> collects = collectMapper1.selectByValueId(valueId);
                PageInfo<Collect> collectPageInfo = new PageInfo<>(collects);
                long total = collectPageInfo.getTotal();
                return BaseData.data(collects,total);
            }else if ("".equals(valueId)&&"".equals(userId)){
                PageHelper.startPage(page,limit);
                CollectExample collectExample = new CollectExample();
                //设置order by 后面的内容
                collectExample.setOrderByClause(sort +" "+order); //add_time desc
                List<Collect> collects = collectMapper.selectByExample(collectExample);
                //可以在查询完成后获得pageInfo → 根据查询结果获得
                PageInfo<Collect> collectPageInfo = new PageInfo<>(collects);
                long total = collectPageInfo.getTotal();//根据sql查询条件执行了一个select count(*)
                return  BaseData.data(collects,total);
            }
        }
        PageHelper.startPage(page,limit);
        CollectExample collectExample = new CollectExample();
        //设置order by后面的内容
        collectExample.setOrderByClause(sort+ " "+order);
        List<Collect> collects = collectMapper.selectByExample(collectExample);
        //可以在查询完成后获得pageInfo →根据查询结果获得
        PageInfo<Collect> collectPageInfo = new PageInfo<>(collects);
        long total = collectPageInfo.getTotal();
        return BaseData.data(collects,total);

    }

    @Autowired
    FootprintMapper footprintMapper;
    @Autowired
    FootprintMapper1 footprintMapper1;

    @Override
    public BaseData footprintControl(Integer page, Integer limit, String userId, String goodsId, String sort, String order) {
        if (goodsId!=null&&userId==null&&!("".equals(goodsId))){
            List<Footprint> footprints = footprintMapper1.selectByGoodsId(goodsId);
            PageInfo<Footprint> footprintPageInfo = new PageInfo<>(footprints);
            long total = footprintPageInfo.getTotal();
            return BaseData.data(footprints,total);

        }else if (goodsId==null&&userId!=null&&!("".equals(userId))){
            List<Footprint> footprints = footprintMapper1.selectByUserId(userId);
            PageInfo<Footprint> footprintPageInfo = new PageInfo<>(footprints);
            long total = footprintPageInfo.getTotal();
            return BaseData.data(footprints,total);
        }
        else if(goodsId!=null&&userId!=null){
            if (!("".equals(goodsId))&&!("".equals(userId))){
                List<Footprint> footprints = footprintMapper1.selectByGoodsIdAndUserId(goodsId, userId);
                PageInfo<Footprint> footprintPageInfo = new PageInfo<>(footprints);
                long total = footprintPageInfo.getTotal();
                return BaseData.data(footprints,total);
            }if (("".equals(goodsId))&&!("".equals(userId))){
                List<Footprint> footprints = footprintMapper1.selectByUserId(userId);
                PageInfo<Footprint> footprintPageInfo = new PageInfo<>(footprints);
                long total = footprintPageInfo.getTotal();
                return BaseData.data(footprints,total);
            }else if (!("".equals(goodsId))&&("".equals(userId))){
                List<Footprint> footprints = footprintMapper1.selectByGoodsId(goodsId);
                PageInfo<Footprint> footprintPageInfo = new PageInfo<>(footprints);
                long total = footprintPageInfo.getTotal();
                return BaseData.data(footprints,total);
            }else if ("".equals(goodsId)&&"".equals(userId)){
                PageHelper.startPage(page,limit);
                FootprintExample footprintExample = new FootprintExample();
                //设置order by 后面的内容
                footprintExample.setOrderByClause(sort +" "+order); //add_time desc
                List<Footprint> footprints = footprintMapper.selectByExample(footprintExample);
                //可以在查询完成后获得pageInfo → 根据查询结果获得
                PageInfo<Footprint> footprintPageInfo = new PageInfo<>(footprints);
                long total = footprintPageInfo.getTotal();//根据sql查询条件执行了一个select count(*)
                return  BaseData.data(footprints,total);
            }
        }
        PageHelper.startPage(page,limit);
        FootprintExample footprintExample = new FootprintExample();
        //设置order by后面的内容
        footprintExample.setOrderByClause(sort+ " "+order);
        List<Footprint> footprints = footprintMapper.selectByExample(footprintExample);
        //可以在查询完成后获得pageInfo →根据查询结果获得
        PageInfo<Footprint> footprintPageInfo = new PageInfo<>(footprints);
        long total = footprintPageInfo.getTotal();
        return BaseData.data(footprints,total);
    }
    @Autowired
    HistoryMapper historyMapper;
    @Autowired
    HistoryMapper1 historyMapper1;

    @Override
    public BaseData historyControl(Integer page, Integer limit, String userId, String keyword, String sort, String order) {
        if (keyword!=null&&userId==null&&!("".equals(keyword))){
            List<History> histories = historyMapper1.selectByKeyword("%" + keyword + "%");
            PageInfo<History> historyPageInfo = new PageInfo<>(histories);
            long total = historyPageInfo.getTotal();
            return BaseData.data(histories,total);
        }else if (keyword==null&&userId!=null&&!("".equals(userId))){
            List<History> histories = historyMapper1.selectByUserId(userId);
            PageInfo<History> historyPageInfo = new PageInfo<>(histories);
            long total = historyPageInfo.getTotal();
            return BaseData.data(histories,total);
        }
        else if(keyword!=null&&userId!=null){
            if (!("".equals(keyword))&&!("".equals(userId))){
                keyword = "%"+keyword+"%";
                List<History> histories = historyMapper1.selectByKeyWordAndUserId(keyword, userId);
                PageInfo<History> historyPageInfo = new PageInfo<>(histories);
                long total = historyPageInfo.getTotal();
                return BaseData.data(histories,total);
            }if (("".equals(keyword))&&!("".equals(userId))){
                List<History> histories = historyMapper1.selectByUserId(userId);
                PageInfo<History> historyPageInfo = new PageInfo<>(histories);
                long total = historyPageInfo.getTotal();
                return BaseData.data(histories,total);
            }else if (!("".equals(keyword))&&("".equals(userId))){
                List<History> histories = historyMapper1.selectByKeyword("%" + keyword + "%");
                PageInfo<History> historyPageInfo = new PageInfo<>(histories);
                long total = historyPageInfo.getTotal();
                return BaseData.data(histories,total);
            }else if ("".equals(keyword)&&"".equals(userId)){
                PageHelper.startPage(page,limit);
                HistoryExample historyExample = new HistoryExample();
                //设置order by 后面的内容
                historyExample.setOrderByClause(sort +" "+order); //add_time desc
                List<History> histories = historyMapper.selectByExample(historyExample);
                //可以在查询完成后获得pageInfo → 根据查询结果获得
                PageInfo<History> historyPageInfo = new PageInfo<>(histories);
                long total = historyPageInfo.getTotal();//根据sql查询条件执行了一个select count(*)
                return  BaseData.data(histories,total);
            }
        }


        PageHelper.startPage(page,limit);
        HistoryExample historyExample = new HistoryExample();
        //设置order by 后面的内容
        historyExample.setOrderByClause(sort +" "+order); //add_time desc
        List<History> histories = historyMapper.selectByExample(historyExample);
        //可以在查询完成后获得pageInfo → 根据查询结果获得
        PageInfo<History> historyPageInfo = new PageInfo<>(histories);
        long total = historyPageInfo.getTotal();//根据sql查询条件执行了一个select count(*)
        return  BaseData.data(histories,total);

    }

    @Autowired
    FeedbackMapper feedbackMapper;
    @Autowired
    FeedbackMapper1 feedbackMapper1;

    @Override
    public Feedback feedbackControl(Integer page, Integer limit, String username, String sort, String order, String id) {
        Feedback feedback = new Feedback();
        if (username!=null&&id==null&&!("".equals(username))){
            List<Feedback.ItemsBean> feedbacks = feedbackMapper1.selectByUserName("%" + username + "%");
            PageInfo<Feedback.ItemsBean> feedbackPageInfo = new PageInfo<>(feedbacks);
            int total = (int) feedbackPageInfo.getTotal();
            feedback.setItems(feedbacks);
            feedback.setTotal(total);
            return feedback;
        }else if (username==null&&id!=null&&!("".equals(id))){
            List<Feedback.ItemsBean> feedbacks = feedbackMapper1.selectById(id);
            PageInfo<Feedback.ItemsBean> feedbackPageInfo = new PageInfo<>(feedbacks);
            int  total = (int) feedbackPageInfo.getTotal();
            feedback.setItems(feedbacks);
            feedback.setTotal(total);
            return feedback;
        }
        else if(username!=null&&id!=null){
            if (!("".equals(username))&&!("".equals(id))){
                username = "%"+username+"%";
                List<Feedback.ItemsBean> feedbacks = feedbackMapper1.selectByUserNameAndId(username, id);
                PageInfo<Feedback.ItemsBean> feedbackPageInfo = new PageInfo<>(feedbacks);
                int total = (int) feedbackPageInfo.getTotal();
                feedback.setItems(feedbacks);
                feedback.setTotal(total);
                return feedback;
            }if (("".equals(username))&&!("".equals(id))){
                List<Feedback.ItemsBean> feedbacks = feedbackMapper1.selectById(id);
                PageInfo<Feedback.ItemsBean> feedbackPageInfo = new PageInfo<>(feedbacks);
                int total = (int) feedbackPageInfo.getTotal();
                feedback.setItems(feedbacks);
                feedback.setTotal(total);
                return feedback;
            }else if (!("".equals(username))&&("".equals(id))){
                List<Feedback.ItemsBean> feedbacks = feedbackMapper1.selectByUserName("%" + username + "%");
                PageInfo<Feedback.ItemsBean> feedbackPageInfo = new PageInfo<>(feedbacks);
                int total = (int) feedbackPageInfo.getTotal();
                feedback.setItems(feedbacks);
                feedback.setTotal(total);
                return feedback;
            }else if ("".equals(username)&&"".equals(id)){
                List<Feedback.ItemsBean> feedbacks = feedbackMapper1.selectByExample();
                //可以在查询完成后获得pageInfo → 根据查询结果获得
                PageInfo<Feedback.ItemsBean> feedbackPageInfo = new PageInfo<>(feedbacks);
                int total = (int) feedbackPageInfo.getTotal();//根据sql查询条件执行了一个select count(*)
                feedback.setItems(feedbacks);
                feedback.setTotal(total);
                return  feedback;
            }
        }
        List<Feedback.ItemsBean> feedbacks = feedbackMapper1.selectByExample();
        //可以在查询完成后获得pageInfo → 根据查询结果获得
        PageInfo<Feedback.ItemsBean> feedbackPageInfo = new PageInfo<>(feedbacks);
        int total = (int) feedbackPageInfo.getTotal();//根据sql查询条件执行了一个select count(*)
        feedback.setItems(feedbacks);
        feedback.setTotal(total);
        return  feedback;
    }

    @Autowired
    DashboardMapper dashboardMapper;

    @Override
    public TotalData dashboardControl() {
        Integer integer = dashboardMapper.selectGoodsTotal();
        Integer integer1 = dashboardMapper.selectUserTotal();
        Integer integer2 = dashboardMapper.selectProductTotal();
        Integer integer3 = dashboardMapper.selectOrderTotal();
        return TotalData.data(integer,integer1,integer2,integer3);
    }



}



